﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using JetBrains.Annotations;
using Volo.Abp;
using Volo.Abp.Domain.Entities;

namespace CommonManagement
{
    /// <summary>
    /// 用户类
    /// </summary>
    public class User : BasicAggregateRoot<int>
    {
        /// <summary>用户ID</summary>
        [Column("UserId")]
        [Key]
        public override int Id { get; protected set; }

        /// <summary>用户名</summary>
        public string UserName { get; protected set; }

        /// <summary>密码</summary>
        public string Password { get; protected set; }

        /// <summary>用户头像</summary>
        public string Avatar { get; protected set; }

        /// <summary>用户昵称</summary>
        public string NickName { get; protected set; }

        /// <summary>用户性别 0 男 1 女</summary>
        public string Sex { get; protected set; }

        /// <summary>电话号码</summary>
        public string Phonenumber { get; protected set; }

        /// <summary>邮箱</summary>
        public string Email { get; protected set; }

        /// <summary>身份证号</summary>
        public string IdCard { get; protected set; }

        /// <summary>账户余额</summary>
        public decimal Balance { get; protected set; }

        /// <summary>用户积分</summary>
        public int Score { get; protected set; }

        /// <summary>余额变动记录集合</summary>
        public IList<Balance> Balances { get; protected set; }

        /// <summary>充值记录集合</summary>
        public IList<Recharge> Recharges { get; protected set; }

        /// <summary>积分记录集合</summary>
        public IList<Score> Scores { get; protected set; }

        /// <summary>获取积分消息集合</summary>
        public IList<Notice> Notices { get; protected set; }

        /// <summary>反馈集合</summary>
        public IList<Feedback> Feedbacks { get; protected set; }

        /// <summary>评论集合</summary>
        public IList<Comment> Comments { get; protected set; }

        /// <summary>订单集合</summary>
        public IList<Order> Orders { get; protected set; }

        private User() { }

        internal User(
            [NotNull] string userName,
            [NotNull] string password,
            string avatar,
            string nickName,
            [NotNull] string sex,
            [NotNull] string phonenumber,
            string email,
            string idCard)
        {
            UserName = Check.NotNullOrWhiteSpace(userName, nameof(userName));
            Password = Check.NotNullOrWhiteSpace(password, nameof(password));
            Avatar = avatar;
            NickName = nickName;
            Sex = Check.NotNullOrWhiteSpace(sex, nameof(sex));
            Phonenumber = Check.NotNullOrWhiteSpace(phonenumber, nameof(phonenumber));
            Email = email;
            IdCard = idCard;
        }

        /// <summary>
        /// 设置用户信息
        /// </summary>
        /// <param name="avatar"></param>
        /// <param name="nickName"></param>
        /// <param name="sex"></param>
        /// <param name="phonenumber"></param>
        /// <param name="email"></param>
        /// <param name="idCard"></param>
        public User SetUser(
            string avatar,
            [NotNull] string nickName,
            string sex,
            string phonenumber,
            string email,
            string idCard)
        {
            if (!string.IsNullOrWhiteSpace(avatar))
                Avatar = avatar;
            NickName = Check.NotNullOrWhiteSpace(nickName, nameof(nickName));
            if (!string.IsNullOrWhiteSpace(sex))
                Sex = sex;
            if (!string.IsNullOrWhiteSpace(phonenumber))
                Phonenumber = phonenumber;
            if (!string.IsNullOrWhiteSpace(email))
                Email = email;
            if (!string.IsNullOrWhiteSpace(idCard))
                IdCard = idCard;
            return this;
        }

        /// <summary>
        /// 设置密码
        /// </summary>
        /// <param name="password"></param>
        public User SetPassword(
            [NotNull] string password)
        {
            Password = Check.NotNullOrWhiteSpace(password, nameof(password));
            return this;
        }

        /// <summary>
        /// 充值
        /// </summary>
        /// <param name="money"></param>
        public User Recharge(int money)
        {
            Balance += money;
            return this;
        }

        /// <summary>
        /// 积分消费
        /// </summary>
        /// <param name="score"></param>
        public User ScoreConsume(int score)
        {
            if (score > Score)
                throw new ScoreNotEnoughException(Score, score);
            Score -= score;
            return this;
        }
    }
}